package com.chhuang.ewcrm.api;

import com.chhuang.ewcrm.config.PropertiesUtil;
import com.chhuang.ewcrm.cons.Constants;
import com.chhuang.ewcrm.utils.MyPath;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * @author chhuang
 * @create 2020-10-23-11:53
 */
public class AppRequest {
    private static Logger logger = LoggerFactory.getLogger(AppRequest.class);
    private AppRequest(String token){
        createService(token);
    }

    private static AppRequest instance;

    private static String BASE_URL;
    private AppService service;
    private static String token;

    public static synchronized AppRequest getInstance(){
        if(instance == null) {
            BASE_URL = PropertiesUtil.getValue(MyPath.getConfigPath()+Constants.PROPERTIES_FILE_NAME, Constants.PS_BASE_URL_STR)+
                    PropertiesUtil.getValue(MyPath.getConfigPath()+Constants.PROPERTIES_FILE_NAME, Constants.PS_API_URL_STR);
            instance = new AppRequest(token);
        }
        return instance;
    }

    public static synchronized void resetInstance(){
        BASE_URL = PropertiesUtil.getValue(MyPath.getConfigPath()+Constants.PROPERTIES_FILE_NAME, Constants.PS_BASE_URL_STR)+
                PropertiesUtil.getValue(MyPath.getConfigPath()+Constants.PROPERTIES_FILE_NAME, Constants.PS_API_URL_STR);
        instance = new AppRequest(token);
    }

    /**
     * build不带header的Retrofit，并创建http服务
     * 在退出登录后要调用这个来重新设置service
     * @return
     */
    public void createService(){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())// 针对rxjava2.x
                .baseUrl(BASE_URL.endsWith("/") ? BASE_URL : BASE_URL+"/")//这里最后一定要有一个/
                .build();
        service = retrofit.create(AppService.class);
    }

    /**
     * build头部带token的Retrofit，并创建http服务
     * 在登录成功后要调用这个来重新设置service
     * @param token
     * @return
     */
    public void createService(final String token){
        if(StringUtils.isEmpty(token)) {
            createService();
            return;
        }
        AppRequest.token = token;
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        //添加拦截器
        httpClient.addInterceptor(chain -> {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .header("Authorization", "Weigh "+token)//添加token
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        });
        OkHttpClient client = httpClient.build();
//        logger.debug("地址："+BASE_URL);
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())// 针对rxjava2.x
                .baseUrl(BASE_URL.endsWith("/") ? BASE_URL : BASE_URL+"/")//这里最后一定要有一个/
                .client(client)
                .build();
        service =  retrofit.create(AppService.class);
    }

    public AppService getService() {
        return service;
    }

}
